import {request} from "../../request/index.js"


const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    pwd: '',
    userId: ''
  },
  getUserInfo: function(e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    }) 
  },

  onLoad: function (options) {
    
  },

  onShow: function () {
    
  },
  gotoRegister() {
    wx.navigateTo({
      url: '../register/register',
    })
  },

  gotoIndexPage() {
    var that = this
    request({
      url: "/user/login", 
      method: 'POST',
      data: {
        userName: that.data.username,
        password: that.data.pwd
      }, 
      header: {
        'content-type': 'application/x-www-form-urlencoded' // post请求默认值
      }
    })
    .then(res => {
      that.setData({
        userId: res.data.data.userId
      })
      app.userId = that.data.userId
      var data = res.data   
      if(data.code === 500) {
        wx.showToast({
          title: data.message,
          icon: 'none',
          mask: true
        })
      } else if(data.code === 200) {
        var roleId = data.data.role
        wx.navigateTo({
          url: '../index/index?roleId=' + roleId + '&userId=' + that.data.userId
        })
        that.setData({
          userId: data.data.userId
        })  
      }
    })
  },
  getusername(e) {
    this.setData({
      username: e.detail.value
    })
  },
  getpwd(e) {
    this.setData({
      pwd: e.detail.value
    }) 
  }
})